const api = require('../../../config/api.js');
const app = getApp();
let that;
/*
 *  后续完善：订单分组、触底加载
 */
Page({
  data: {
    ImgRootUrl: app.globalData.ImgRootUrl,
    orderList: []
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    that = this;
  },
  getOrderList() {
    app.util.wxrequest(api.OrderList).then(function (res) {
      if (res.errno === 0) {
        console.log(res.data);
        that.setData({
          orderList: res.data
        });
      } else {
        app.util.showToast(res.error);
        that.timer = setTimeout(function () {
          clearTimeout(that.timer);
          wx.switchTab({
            url: '/pages/ucenter/index/index'
          });
        }, 1500);
      }
    });
  },
  payOrder(event) {
    const orderId = event.target.dataset.order_id;
    const actualPrice = event.target.dataset.order_amount;
    wx.navigateTo({
      url: '/pages/pay/pay?orderId=' + orderId + '&actualPrice=' + actualPrice,
    })
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
    this.getOrderList();
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  }
})